<?php

namespace App\Services;

use App\Facades\UniqID;
use App\Repositories\Contracts\UserRepository;
use Tymon\JWTAuth\Facades\JWTAuth;

class UserService
{
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            $result = $this->userRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->userRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function login($openid)
    {
        try {
            // 根据openid查询用户, 没找到则新建
            $userData = $this->userRepository->findByFilter([
                'mini_openid' => $openid,
            ]);
            if (empty($userData['user'])) {
                $userData = $this->add([
                    'mini_openid' => $openid,
                ]);
            }
            $tokenObj = JWTAuth::fromUser($userData['user']);
            $result['token'] = $tokenObj;
            $result['user'] = $userData['data']['data'];

            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            $data = [
                'mini_openid' => $param['mini_openid'],
                'nickname' => $param['nickname'] ?? '默认昵称',
                'sex' => $param['sex'] ?? 0,
                'avatar' => $param['avatar'] ?? '',
                'phone' => $param['phone'] ?? '',
                'user_id' => UniqID::nextId(),
            ];

            $user = $this->userRepository->store($data);

            return $user['data'];
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param)
    {
        try {
            $data = [
                'nickname' => $param['nickname'],
                'avatar' => $param['avatar'],
            ];

            if (isset($param['sex'])) {
                $data['sex'] = $param['sex'];
            }
            $user = $this->userRepository->updateByFilter($data, ['user_id' => $param['uid']]);
            $result = [];
            if ($user > 0) {
                $userData = $this->userRepository->findByFilter(['user_id' => $param['uid']]);
                $result['user'] = $userData['data']['data'];
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $user = $this->userRepository->delete($id);

            $result = [];
            if ($user) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
